package com.naiterui.ehp.bs.cms.modules.pharmacy.controller;

import java.util.Set;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.pharmacy.feign.WarehouseGoodsFeignClient;
import com.naiterui.ehp.bs.cms.modules.pharmacy.service.IWarehouseGoodsService;
import com.naiterui.ehp.bs.cms.modules.pharmacy.vo.WarehouseGoodsVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.PHARMACY})
@RestController
@RequestMapping("pharmacy/warehouse/goods")
public class WarehouseGoodsController {

  @Autowired
  private IWarehouseGoodsService warehouseGoodsService;
  @Autowired
  private WarehouseGoodsFeignClient warehouseGoodsFeignClient;

  /**
   * 商品查询
   *
   * @return
   */
  @SysLog("商品查询:cms中录入的商品")
  @GetMapping("/list")
  @ApiOperation("商品查询，cms中录入的商品")
  @PreAuthorize("@p.check('pharmacy:partner:goods:list')")
  public PageVO<WarehouseGoodsVO> list(PageParamsVO pageParamsVO, @Valid WarehouseGoodsVO warehouseGoodsVO) {
    return this.warehouseGoodsService.queryPage(pageParamsVO, warehouseGoodsVO);
  }

  /**
   * 已选合作商品
   *
   * @return
   */
  @SysLog("已选合作商品")
  @GetMapping("/select/list/{warehouseId}")
  @ApiOperation("已选合作商品")
  @ApiImplicitParam(name = "warehouseId", value = "商家ID", dataType = "long", paramType = "path", required = true)
  @PreAuthorize("@p.check('pharmacy:partner:goods:list')")
  public PageVO<WarehouseGoodsVO> selectList(PageParamsVO pageParamsVO, @PathVariable("warehouseId") Long warehouseId) {
    return this.warehouseGoodsService.selectList(pageParamsVO, warehouseId);
  }

  /**
   * 删除合作商品
   */
  @SysLog("删除合作商品")
  @DeleteMapping("/select/list/{warehouseId}")
  @ApiOperation("删除合作商品")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "skuIds", value = "记录ID数组", dataType = "int", paramType = "body", required = true),
  })
  @PreAuthorize("@p.check('pharmacy:partner:delete')")
  public void delSelectList(@RequestBody Set<Long> skuIds,
      @PathVariable("warehouseId") Long warehouseId) throws BusinessException {
    this.warehouseGoodsFeignClient.delSelectList(warehouseId, skuIds);
  }

  /**
   * 设置合作商品
   */
  @SysLog("设置合作商品")
  @PostMapping("/select/list/{warehouseId}")
  @ApiOperation("设置合作商品")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "skuIds", value = "记录ID数组", dataType = "int", paramType = "body", required = true),
  })
  @PreAuthorize("@p.check('pharmacy:partner:set')")
  public void setSelectList(@RequestBody Set<Long> skuIds, @PathVariable("warehouseId") Long warehouseId) throws BusinessException {
    this.warehouseGoodsFeignClient.setSelectList(warehouseId, skuIds);
  }

}
